package org.example.project_12.controller;

import org.example.project_12.dto.CreateDocumentDto;
import org.example.project_12.dto.DocumentDto;
import org.example.project_12.dto.Result;
import org.example.project_12.dto.UpdateDocumentDto;
import org.example.project_12.service.DocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/document")
public class DocumentController {
    
    @Autowired
    private DocumentService documentService;
    
    /**
     * 创建文档
     */
    @PostMapping("/create")
    public Result createDocument(@RequestBody CreateDocumentDto createDocumentDto, 
                                            @RequestParam Long authorId) {
        try {
            DocumentDto document = documentService.createDocument(createDocumentDto, authorId);
            return Result.ok(document);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
    
    /**
     * 获取文档详情
     */
    @GetMapping("/{documentId}")
    public Result getDocument(@PathVariable Long documentId, 
                                         @RequestParam Long userId) {
        try {
            DocumentDto document = documentService.getDocumentById(documentId, userId);
            return Result.ok(document);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
    
    /**
     * 根据作者获取文档列表
     */
    @GetMapping("/author/{authorId}")
    public Result getDocumentsByAuthor(@PathVariable Long authorId, 
                                                         @RequestParam Long currentUserId) {
        try {
            List<DocumentDto> documents = documentService.getDocumentsByAuthor(authorId, currentUserId);
            return Result.ok(documents);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
    
    /**
     * 根据空间获取文档列表
     */
    @GetMapping("/space/{spaceId}")
    public Result getDocumentsBySpace(@PathVariable Long spaceId, 
                                                        @RequestParam Long currentUserId) {
        try {
            List<DocumentDto> documents = documentService.getDocumentsBySpace(spaceId, currentUserId);
            return Result.ok(documents);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
    
    /**
     * 获取公开文档列表
     */
    @GetMapping("/public")
    public Result getPublicDocuments(@RequestParam Long currentUserId) {
        try {
            List<DocumentDto> documents = documentService.getPublicDocuments(currentUserId);
            return Result.ok(documents);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
    
    /**
     * 搜索文档
     */
    @GetMapping("/search")
    public Result searchDocuments(@RequestParam String title, 
                                                    @RequestParam Long currentUserId) {
        try {
            List<DocumentDto> documents = documentService.searchDocumentsByTitle(title, currentUserId);
            return Result.ok(documents);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
    
    /**
     * 更新文档
     */
    @PutMapping("/{documentId}")
    public Result updateDocument(@PathVariable Long documentId, 
                                            @RequestBody UpdateDocumentDto updateDocumentDto, 
                                            @RequestParam Long userId) {
        try {
            DocumentDto document = documentService.updateDocument(documentId, updateDocumentDto, userId);
            return Result.ok(document);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
    
    /**
     * 删除文档
     */
    @DeleteMapping("/{documentId}")
    public Result deleteDocument(@PathVariable Long documentId, 
                                        @RequestParam Long userId) {
        try {
            boolean result = documentService.deleteDocument(documentId, userId);
            return Result.ok(result);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
    
    /**
     * 锁定文档
     */
    @PostMapping("/{documentId}/lock")
    public Result lockDocument(@PathVariable Long documentId, 
                                      @RequestParam Long userId) {
        try {
            boolean result = documentService.lockDocument(documentId, userId);
            return Result.ok(result);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
    
    /**
     * 解锁文档
     */
    @PostMapping("/{documentId}/unlock")
    public Result unlockDocument(@PathVariable Long documentId, 
                                        @RequestParam Long userId) {
        try {
            boolean result = documentService.unlockDocument(documentId, userId);
            return Result.ok(result);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
    
    /**
     * 收藏文档
     */
    @PostMapping("/{documentId}/favorite")
    public Result favoriteDocument(@PathVariable Long documentId, 
                                          @RequestParam Long userId) {
        try {
            boolean result = documentService.favoriteDocument(documentId, userId);
            return Result.ok(result);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
    
    /**
     * 取消收藏文档
     */
    @DeleteMapping("/{documentId}/favorite")
    public Result unfavoriteDocument(@PathVariable Long documentId, 
                                            @RequestParam Long userId) {
        try {
            boolean result = documentService.unfavoriteDocument(documentId, userId);
            return Result.ok(result);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
    
    /**
     * 获取用户收藏的文档列表
     */
    @GetMapping("/favorites")
    public Result getFavoriteDocuments(@RequestParam Long userId) {
        try {
            List<DocumentDto> documents = documentService.getFavoriteDocuments(userId);
            return Result.ok(documents);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
    
    /**
     * 导出文档为TXT
     */
    @GetMapping("/{documentId}/export")
    public Result exportDocument(@PathVariable Long documentId, 
                                       @RequestParam Long userId) {
        try {
            String content = documentService.exportDocumentAsTxt(documentId, userId);
            return Result.ok(content);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }
} 